<template>
  <div class="user-manage">
    <div class="header">
      <h2>用户管理</h2>
    </div>

    <el-table 
      :data="userList" 
      border 
      style="width: 100%"
      v-loading="loading"
    >
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="nickname" label="昵称" />
      <el-table-column prop="avatar" label="头像">
        <template #default="{ row }">
          <el-avatar :src="row.avatar" />
        </template>
      </el-table-column>
      <el-table-column prop="points" label="积分" width="100" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="{ row }">
          <el-switch
            v-model="row.status"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" fixed="right">
        <template #default="{ row }">
          <el-button type="primary" size="small" @click="handleEdit(row)">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      title="编辑用户"
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname" />
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <el-input v-model="form.avatar" />
        </el-form-item>
        <el-form-item label="积分" prop="points">
          <el-input-number v-model="form.points" :min="0" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '@/utils/request'

// 列表数据
const loading = ref(false)
const userList = ref([])
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)

// 编辑表单
const dialogVisible = ref(false)
const formRef = ref(null)
const form = ref({
  id: '',
  nickname: '',
  avatar: '',
  points: 0,
  status: 1
})

// 表单验证规则
const rules = {
  nickname: [
    { required: true, message: '请输入昵称', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  avatar: [
    { required: true, message: '请输入头像地址', trigger: 'blur' }
  ],
  points: [
    { required: true, message: '请输入积分', trigger: 'change' }
  ]
}

// 获取用户列表
const fetchUserList = async () => {
  loading.value = true
  try {
    const response = await request.get('/api/v1/medical/user/query_all_user')
    if (response.code === '0000') {
      userList.value = response.data
      total.value = userList.value.length
    }
  } catch (error) {
    console.error('获取用户列表失败:', error)
    ElMessage.error('获取用户列表失败')
  } finally {
    loading.value = false
  }
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  fetchUserList()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchUserList()
}

// 修改用户状态
const handleStatusChange = async (row) => {
  try {
    const response = await request.post('/api/v1/medical/user/update_user_status', {
      id: row.id,
      status: row.status
    })
    
    if (response.code === '0000') {
      ElMessage.success('状态修改成功')
      fetchUserList()
    } else {
      row.status = row.status === 1 ? 0 : 1 // 恢复原状态
      ElMessage.error('状态修改失败')
    }
  } catch (error) {
    row.status = row.status === 1 ? 0 : 1 // 恢复原状态
    console.error('修改用户状态失败:', error)
    ElMessage.error('修改用户状态失败')
  }
}

// 编辑用户
const handleEdit = async (row) => {
  try {
    const response = await request.post('/api/v1/medical/user/query_user_detail', {
      id: row.id
    })
    
    if (response.code === '0000') {
      form.value = {
        id: response.data.id,
        nickname: response.data.nickname,
        avatar: response.data.avatar,
        points: response.data.points,
        status: response.data.status
      }
      dialogVisible.value = true
    }
  } catch (error) {
    console.error('获取用户详情失败:', error)
    ElMessage.error('获取用户详情失败')
  }
}

// 保存用户信息
const handleSave = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    const response = await request.post('/api/v1/medical/user/update_user_info', form.value)
    
    if (response.code === '0000') {
      ElMessage.success('保存成功')
      dialogVisible.value = false
      fetchUserList()
    }
  } catch (error) {
    console.error('保存用户信息失败:', error)
    ElMessage.error('保存失败')
  }
}

onMounted(() => {
  fetchUserList()
})
</script>

<style lang="scss" scoped>
.user-container {
  padding: 20px;
  
  .header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
    
    .search {
      .el-input {
        width: 300px;
      }
    }
  }
  
  .pagination {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
  
  .avatar-uploader {
    display: flex;
    justify-content: center;
    
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 100px;
      height: 100px;
      text-align: center;
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      
      &:hover {
        border-color: #409EFF;
      }
      
      .el-icon {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }
    }
  }
}
</style> 